package com.iplatform.iplatformuser.privilege.service.impl;

import com.iplatform.commons.utils.CommResponse;
import com.iplatform.iplatformuser.privilege.dao.SysdataMapper;
import com.iplatform.module.privilege.PriMenuBean;
import com.iplatform.iplatformuser.privilege.dao.PriMenuMapper;
import com.iplatform.iplatformuser.privilege.service.inter.IPriMenuService;
import com.iplatform.module.sys.SysDataBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Title: PriMenuServiceImpl
 * Description:
 *
 * @author RayLLi
 * @date 2018年8月27日
 */
@Service
public class PriMenuServiceImpl implements IPriMenuService {

    @Autowired
    public PriMenuMapper priMenuMapper;

    @Autowired
    private SysdataMapper sysdataMapper;

    @Override
    public List<PriMenuBean> queryMenu(Map<String, String> formMap) {
        return priMenuMapper.queryMenu(formMap);
    }

    @Override
    public List<PriMenuBean> queryMenuListByLevel(Map<String, String> formMap) {
        List<PriMenuBean> currentMenuList = priMenuMapper.queryMenuListByLevel(formMap);

        return currentMenuList;
    }

    /**
     * Title: initTree
     * Description: 组合树形结构上下级ID
     *
     * @param resultList
     */
    private void initTree(List<PriMenuBean> resultList) {
        for (int i = 0; i < resultList.size(); i++) {
            PriMenuBean currentMenuBean = resultList.get(i);

            if ("0".equals(currentMenuBean.getParentId())) {
                currentMenuBean.setTreeParentsId("0");
                currentMenuBean.setTreeId(String.valueOf(currentMenuBean.getMenuOrd()).trim());
            } else {
                currentMenuBean.setTreeParentsId(currentMenuBean.getAllParentsId().trim());
                currentMenuBean.setTreeId((currentMenuBean.getAllParentsId() + "-" + currentMenuBean.getMenuOrd()).trim());
            }

        }
    }

    @Override
    @Transactional
    public int insertMenu(PriMenuBean priMenuBean) {
        //设置默认图标
        if (null == priMenuBean.getIcon() || "".equals(priMenuBean.getIcon())) {
            SysDataBean sysDataBean = sysdataMapper.isCodeExist("ICON_TYPE", "0", "");
            if (!StringUtils.isEmpty(sysDataBean)) {
                priMenuBean.setIcon(sysDataBean.getIcon());
            }
        }
        useOrdNo(priMenuBean);
        if(!org.apache.commons.lang.StringUtils.isBlank(priMenuBean.getResourceMenuId())){
           //删除
            priMenuMapper.deleteMenuWithResourceMenuId(priMenuBean.getResourceMenuId());
        }
        return priMenuMapper.insertMenu(priMenuBean);
    }

    /**
     * 操作排序码
     * @param priMenuBean
     */
    public void useOrdNo(PriMenuBean priMenuBean){
        List<Integer> menuOrds = priMenuMapper.findMenuOrd(priMenuBean.getParentId(),priMenuBean.getMenuId());
        if (menuOrds.contains(priMenuBean.getMenuOrd())){
            List<String> menuIds = priMenuMapper.findByMenuOrd(priMenuBean.getMenuOrd(),priMenuBean.getParentId(),priMenuBean.getMenuId());
            priMenuMapper.menuOrdAddOne(menuIds);
        }
    }

    @Override
    @Transactional
    public int updateMenu(PriMenuBean priMenuBean) {
        useOrdNo(priMenuBean);
        return priMenuMapper.updateMenu(priMenuBean);
    }

    @Override
    @Transactional
    public int deleteMenu(String deleteId) {
        return priMenuMapper.deleteMenu(deleteId);
    }

    @Override
    public void batchDelete(String[] deleteIds) {
        priMenuMapper.batchDeleteMenu(deleteIds);
        priMenuMapper.batchDeleteChildsMenu(deleteIds);
    }

    @Override
    @Transactional
    public void menuSelectedOpt(String menuId, String[] optIds, String createdBy) {

        try {
            priMenuMapper.deleteSelectedOpt(menuId);
//			priMenuMapper.deleteResOpt(menuId);
            if (optIds != null) {
                for (String optId : optIds) {
                    String menuOptId = UUID.randomUUID().toString().replaceAll("-", "");
                    Map<String, String> formMap = new HashMap<String, String>(4);
                    formMap.put("menuOptId", menuOptId);
                    formMap.put("menuId", menuId);
                    formMap.put("optId", optId);
                    formMap.put("createdBy", createdBy);
                    formMap.put("version", "1");
                    formMap.put("is_deleted", "N");
                    priMenuMapper.updateBindFunction(formMap);
//					priMenuMapper.updateResOpt(formMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public List<Map<String, String>> querySelectOptByMenuId(String menuId) {
        return priMenuMapper.querySelectOptByMenuId(menuId);
    }

    @Deprecated
    @Override
    public void setIsHome(String menuId) {
//		priMenuMapper.cancelAllHomePage();
//		priMenuMapper.setupOrCancelHomePageByMenuId(menuId);
    }

    @Override
    public List<Map<String, String>> listSelectWithOutRole(Map<String, String> formMap) {
        return priMenuMapper.listSelectWithOutRole(formMap);
    }

    @Override
    public List<Map<String, String>> listSelectWithInRole(Map<String, String> formMap) {
        return priMenuMapper.listSelectWithInRole(formMap);
    }

    @Override
    @Transactional
    public void updateHomeRole(String menuId, String checkedIds) {
//		priMenuMapper.deleteHomeWithMenuId(menuId);
        if (!StringUtils.isEmpty(checkedIds)) {
            for (String roleId : checkedIds.split(",")) {
//				priMenuMapper.deleteHomeWithRoleId(roleId);
                Map<String, String> formMap = new HashMap<String, String>(4);
                formMap.put("MENU_ID", menuId);
                formMap.put("ROLE_ID", roleId);
                priMenuMapper.updateHomeRole(formMap);
            }
        }
    }

    @Override
    public List<PriMenuBean> queryMenuListByLayer(Map<String, String> formMap) {
        List<PriMenuBean> topMenuList = priMenuMapper.queryTopMenuListByLayer(formMap);
        return queryMenuListByParentId(topMenuList, formMap);
    }

    private List<PriMenuBean> queryMenuListByParentId(List<PriMenuBean> menuList, Map<String, String> formMap) {
        for (PriMenuBean menu : menuList) {
            formMap.put("parentId", menu.getMenuId());
            menu.setPriMenuBeans(priMenuMapper.queryMenuListByParentId(formMap));
        }
        return menuList;
    }

    @Override
    public List<Map<String, String>> listForTree(Map<String, String> formMap) {
        return priMenuMapper.listForTree(formMap);
    }

    @Override
    public List<PriMenuBean> querySelectedByName(Map<String, String> formMap) {
        return priMenuMapper.querySelectedByName(formMap);
    }

    @Override
    public List<Map<String, String>> queryMenuName() {
        return priMenuMapper.queryMenuName();
    }

    @Override
    public List<PriMenuBean> queryMenuByParentId(Map<String, String> formMap) {
        if (StringUtils.isEmpty(formMap.get("parentId"))) {
            return fuzzyQueryMenu(formMap);
        } else {
            List<PriMenuBean> topMenuList = priMenuMapper.queryMenuListByParentId(formMap);
            return queryMenuListByParentId(topMenuList, formMap);
        }

    }

    @Override
    public List<String> queryMenuByBookId(List<String> bookIds) {
        return priMenuMapper.queryMenuByBookId(bookIds);
    }

    @Override
    public List<PriMenuBean> queryChildsMenu(String parentId) {
        return priMenuMapper.queryChildsMenu(parentId);
    }

    @Override
    public List<PriMenuBean> fuzzyQueryMenu(Map<String, String> formMap) {

        return priMenuMapper.fuzzyQueryMenu(formMap);
    }

    @Override
    public CommResponse updateStatus(String status, String menuId) {
        //先修改父菜单状态
        priMenuMapper.updateStatus(status, menuId);
        //再修改所有相关子菜单状态
        getAllChildsMenuId(status, menuId);
        return new CommResponse("菜单更改状态成功", true);
    }

    @Override
    public CommResponse delMenu(String menuId) {
        priMenuMapper.delMenu(menuId);
        return new CommResponse("删除成功", true);
    }

    @Override
    public int queryChildMenuNum(String menuId) {
        return priMenuMapper.queryChildMenuNum(menuId);
    }
    
    @Override
    public PriMenuBean findMenuIdBySysDataCodeType(String typeCode) {
        return priMenuMapper.findMenuIdBySysDataCodeType(typeCode);
    }



    public List<String> getAllChildsMenuId(String status, String menuId) {
        //查出子菜单
        List<String> childs = priMenuMapper.queryChildMenuId(menuId);
        if (childs.size() > 0) {
            for (String child : childs) {
                priMenuMapper.updateStatus(status, child);
                getAllChildsMenuId(status, child);
            }
        }
        return childs;
    }

    @Override
    public Integer queryMaxOrd(String parent_id) {
        Integer maxOrd = priMenuMapper.queryMaxOrd(parent_id);
        return maxOrd;
    }


}
